package com.javasm.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author: Lisa
 * @className: SpringUtil
 * @description:
 * @date: 2023/10/22 22:43
 * @VERSION: 0.1
 * @SINCE: jdk11
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        applicationContext = ac;
    }

    public static <T> T getBean(Class<T> clz){
        return applicationContext.getBean(clz);
    }

    public static <T> T getBean(String name,Class<T> clz){
        return applicationContext.getBean(name,clz);
    }
}
